package com.chuanyang.driverh.mainhome.fragment;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainhome.FilterActivity;
import com.chuanyang.driverh.mainhome.adapter.CarPlanAdapter;
import com.chuanyang.driverh.response2.RequestFilter;
import com.chuanyang.driverh.response2.ResultCarPlan;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.FilterEvent;
import com.chuanyang.driverh.utils.eventbus.RefleshFragEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


public class CarPlanFragment extends BaseListFragment {
    private CarPlanAdapter listAdapter;
    private View title_bar;
    private RadioGroup rg_top;
    private RadioButton rb0,rb1,rb2,rb3;
    private RequestFilter filter;
    private String showType="4";//1 累计接单 2当天接单 3当天搁置 4全部订单
    @Override
    public BaseListAdapter createAdapter() {
        return listAdapter = new CarPlanAdapter(getActivity());
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        title_bar = View.inflate(getContext(), R.layout.home_header_get_order, null);
        rg_top=(RadioGroup) title_bar.findViewById(R.id.rg_top);
        rb0=(RadioButton) title_bar.findViewById(R.id.rb0);
        rb1=(RadioButton) title_bar.findViewById(R.id.rb1);
        rb2=(RadioButton) title_bar.findViewById(R.id.rb2);
        rb3=(RadioButton) title_bar.findViewById(R.id.rb3);

        if (root.getChildCount()>0)root.removeAllViews();
        root.addView(title_bar,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    @Override
    protected void initData(){
        filter=new RequestFilter();
        rb0.setText("全部订单(吨)"+"\n"+"0");
        rb1.setText("待派单(吨)"+"\n"+"0");
        rb2.setText("派单中(吨)"+"\n"+"0");
        rb3.setText("完结(吨)"+"\n"+"0");
        onRefresh() ;
    }

    @Override
    protected void initListener() {
        rg_top.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb0:
                        showType="4";
                        break;
                    case R.id.rb1:
                        showType="1";
                        break;
                    case R.id.rb2:
                        showType="3";
                        break;
                    case R.id.rb3:
                        showType="2";
                        break;
                }
                filter = new RequestFilter();
                onRefresh();
            }
        });

    }

    public void filter(){
        Intent intent=new Intent(activityContext, FilterActivity.class);
        intent.putExtra("type","2");
        intent.putExtra("data",filter);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    /**
     * 根据车队id查询派单
     */
    private void getData() {
        getEmptyView().setVisibility(View.GONE);
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("fleetId", SharedPrefUtil.getFleetId());

        params.put("page", mCurrentPage + "");
        params.put("length", "10");
        if(!"4".equals(showType))
        params.put("showType", showType);
        if (!StringUtil.isBlank(filter.getStartPoint())){
            params.put("beginCity",filter.getStartPoint());
        }
        if (!StringUtil.isBlank(filter.getEndPoint())){
            params.put("endCity",filter.getEndPoint());
        }
        if (!StringUtil.isBlank(filter.getStartTime())){
            params.put("orderTimes",filter.getStartTime());
        }
        if (!StringUtil.isBlank(filter.getEndTime())){
            params.put("orderTimee",filter.getEndTime());

        }
        if (!StringUtil.isBlank(filter.getSelectType())){
            params.put("selectType",filter.getSelectType());

        }
        if (filter.getOrderStatus2()!=null && filter.getOrderStatus2().size()>0){
            params.put("onStatuss",StringUtil.getSplitString(filter.getOrderStatus2()));
        }
        if (filter.getOrderType2()!=null &&filter.getOrderType2().size()>0){
//            params.put("orderType",filter.getOrderType2());
            params.put("orderTypes",StringUtil.getSplitString(filter.getOrderType2()));
        }

        MyHttpUtil.getDispatchOrder(getActivity(),params, new MyCallback<ResultCarPlan>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id){
                springView.onFinishFreshAndLoad();
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                onShowFailed();
            }
            @Override
            public void onSuccess(ResultCarPlan result, int id) {
                if (result.getCode() == ConstUtil.OK) {
//                    List<CarPlanResult.CarPlanEntityOut.carPlanEntityMiddle.InnerEntity> ss = result.getData().getData().getData();
                    try {
                        setDataResult(result.getData().getData().getData());
                        setHeader(result.getData().getDispatchInfo());
                    } catch (Exception e) {
                        if (result.getMsg()!=null){
                            PrintUtil.toast(activityContext,result.getMsg());
                        }
                        listAdapter.clear();
                        listAdapter.notifyDataSetChanged();
                    }

                }else{
                    onShowNull();
                    springView.onFinishFreshAndLoad();
                }
            }
        });
    }

    private void setHeader(ResultCarPlan.CarPlanEntityOut.DispatchInfoEntity dispatchInfo) {
        if (dispatchInfo==null)return;
        rb0.setText("全部订单(吨)"+"\n"+StringUtil.getWeight(dispatchInfo.getAllWeigth()+""));
        rb1.setText("待派单(吨)"+"\n"+StringUtil.getWeight(dispatchInfo.getWaitDispatchWeigth()+""));
        rb2.setText("派单中(吨)"+"\n"+StringUtil.getWeight(dispatchInfo.getInTheDispatchWeigth()+""));
        rb3.setText("已完结(吨)"+"\n"+StringUtil.getWeight(dispatchInfo.getThenDispatchWeigth()+""));
    }

    @Override
    public void onGetMore() {
        getData();
    }
    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
    }

    @Override
    protected void onForceRefresh() {
        onRefresh();
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }



    public void onEventMainThread(FilterEvent event) {
        if ("2".equals(event.getType()) ){
           filter=event.getData();
            onRefresh();
        }
    }
    public void onEventMainThread(RefleshFragEvent event) {
        if (event.getIndex()==2 ){
            onRefresh();
        }
    }
}
